package com.slice.upload.task;

import com.slice.upload.util.ThreadUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * @ClassName SimpleAsyncExecutor
 * @Description 自定义线程池执行器
 * @Author frinder
 * @Date 2021/1/27 15:12
 * @Verion 1.0
 */
@Slf4j
public class SimpleAsyncExecutor {

    public final static SimpleAsyncExecutor INSTANCE = new SimpleAsyncExecutor();

    private final ExecutorService executor = ThreadUtils.getThreadPoolExecutor(5);

    private SimpleAsyncExecutor() {
    }

    public <T> T exec(Callable task) {
        T t = null;
        Future<T> future = executor.submit(task);
        try {
            t = future.get();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return t;
    }

    public void exec(Runnable task) {
        executor.submit(task);
    }

}